﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Vehicle_Maintenance
{
    public partial class Service_Log : Form
    {

        private bool dPoint = false;
        private bool tPoint = false;
        private bool updating = false;

        public Service_Log(bool update = false, string vin = "", string date = "", string serType = "")
        {
            InitializeComponent();
            updating = update;
            if (update)
            {
                DataTable data = this.servicesTableAdapter.GetDataByServiceInfo(vin, Convert.ToDateTime(date), serType);
                DataTable SRData = this.serviceRemindersTableAdapter.GetDataBySRInfo(vin, Convert.ToDateTime(date), serType);

                if (data.Rows.Count == 1)
                {
                    DataRow dRow = data.Rows[0];
                    tb_Odom.Text = dRow["Odom"].ToString();
                    tb_Total.Text = dRow["T_Cost"].ToString();
                    dt_SerDate.Value = Convert.ToDateTime(dRow["DateTime"].ToString());
                    for (int i = 0; i < cb_SerType.Items.Count; i++)
                    {
                        if (cb_SerType.Items[i].Equals(dRow["Serv_type"].ToString()))
                        {
                            cb_SerType.SelectedIndex = i;
                            break;
                        }
                        if (i == cb_SerType.Items.Count - 1)
                        {
                            cb_SerType.SelectedIndex = i;
                            tb_SOther.Text = dRow["Serv_type"].ToString();
                        }
                    }
                    if (dRow["Loc"].ToString() != "")
                    {
                        tb_Loaction.Text = dRow["Loc"].ToString();
                    }
                    if (dRow["Pay_type"].ToString() != "")
                    {
                        for (int i = 0; i < cb_PayType.Items.Count; i++)
                        {
                            if (cb_PayType.Items[i].Equals(dRow["Pay_type"].ToString()))
                            {
                                cb_PayType.SelectedIndex = i;
                                break;
                            }
                            if (i == cb_PayType.Items.Count - 1)
                            {
                                cb_PayType.SelectedIndex = i;
                                tb_POther.Text = dRow["Pay_type"].ToString();
                            }
                        }
                    }
                    if (SRData.Rows.Count == 1)
                    {
                        DataRow SRrow = SRData.Rows[0];
                        chb_SerReminder.Checked = true;
                        int months = Convert.ToInt32(SRrow["Months"].ToString());
                        double miles = Convert.ToDouble(SRrow["Odom"].ToString()) - Convert.ToDouble(tb_Odom.Text);
                        tb_Miles.Text = miles.ToString();
                        num_Months.Value = months;
                    }
                }
            }
        }

        private void Service_Log_Load(object sender, EventArgs e)
        {

        }

        private void b_OK_Click(object sender, EventArgs e)
        {
            if (tb_Odom.Text == "")
            {
                MessageBox.Show("Odometer is required", "Required Field", MessageBoxButtons.OK);
            }
            else if (cb_SerType.SelectedIndex == -1)
            {
                MessageBox.Show("A Service Type is required", "Required Field", MessageBoxButtons.OK);
            }
            else if (tb_Total.Text == "")
            {
                MessageBox.Show("Total is required", "Required Field", MessageBoxButtons.OK);
            }
            else
            {
                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }

        private void cb_SerType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cb_SerType.SelectedIndex.Equals(10))
            {
                tb_SOther.Visible = true;
            }
            else
            {
                tb_SOther.Visible = false;
            }
        }

        #region Select weither you want a service reminder or not
        private void cb_PayType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cb_PayType.SelectedIndex.Equals(6))
            {
                tb_POther.Visible = true;
            }
            else
            {
                tb_POther.Visible = false;
            }
        }

        private void chb_SerReminder_CheckedChanged(object sender, EventArgs e)
        {
            if (chb_SerReminder.Checked == true)
            {
                tb_Miles.Enabled = true;
                num_Months.Enabled = true;
            }
            else
            {
                tb_Miles.Enabled = false;
                num_Months.Enabled = false;
            }
        } 
        #endregion

        #region make certain textboxs only output numbers
        private void tb_Miles_KeyPress(object sender, KeyPressEventArgs e)
        {
            char a = e.KeyChar;

            if (Char.IsLetter(a) | Char.IsPunctuation(a) |
                Char.IsSymbol(a))
            {
                e.Handled = true;
            } // end if    
        }

        private void tb_Odom_KeyPress(object sender, KeyPressEventArgs e)
        {
            char a = e.KeyChar;

            if (0x08 == a && tb_Odom.Text.Length != 0)
            {
                if (tb_Odom.Text[tb_Odom.Text.Length - 1] == '.')
                {
                    dPoint = false;
                }
            }

            if (Char.IsLetter(a) | Char.IsPunctuation(a) |
                Char.IsSymbol(a) | ((tb_Odom.TextLength >= 8 && dPoint) | (tb_Odom.TextLength >= 6 && !dPoint)))
            {
                if (a == '.' && !dPoint)
                {
                    dPoint = true;
                }
                else if (0x08 != a)
                {
                    e.Handled = true;
                }
            } // end if    
        }

        private void tb_Total_KeyPress(object sender, KeyPressEventArgs e)
        {
            char a = e.KeyChar;

            if (0x08 == a && tb_Total.Text.Length != 0)
            {
                if (tb_Total.Text[tb_Total.Text.Length - 1] == '.')
                {
                    tPoint = false;
                }
            }

            if (Char.IsLetter(a) | Char.IsPunctuation(a) |
                Char.IsSymbol(a) | ((tb_Total.TextLength >= 10 && tPoint) | (tb_Total.TextLength >= 7 && !tPoint)))
            {
                if (a == '.' && !tPoint)
                {
                    tPoint = true;
                }
                else if (0x08 != a)
                {
                    e.Handled = true;
                }
            } // end if    
        } 
        #endregion
    }
}
